<?php
// 父类
class Person {
    public $name;
    protected $age;
    
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }
    
    public function sayHello() {
        return "你好，我是{$this->name}，今年{$this->age}岁。";
    }
    
    public function getAge() {
        return $this->age;
    }
}

// 子类继承父类
class Student extends Person {
    private $studentId;
    private $major;
    
    public function __construct($name, $age, $studentId, $major) {
        parent::__construct($name, $age);
        $this->studentId = $studentId;
        $this->major = $major;
    }
    
    public function study() {
        return "{$this->name}正在学习{$this->major}专业的课程。";
    }
    
    // 重写父类方法
    public function sayHello() {
        return parent::sayHello() . "我是一名学生，学号是{$this->studentId}。";
    }
}

// 创建子类对象
$student = new Student("李四", 20, "2023001", "计算机科学");
echo $student->sayHello() . "\n";
echo $student->study() . "\n";

// 多态性示例
function greet(Person $person) {
    echo $person->sayHello() . "\n";
}

$person = new Person("张三", 30);
$student = new Student("李四", 20, "2023001", "计算机科学");

greet($person);  // 调用Person的sayHello方法
greet($student); // 调用Student的sayHello方法（多态性）
?>